'''
Created on Dec 12, 2010

@author: Shpiz
'''
import re
from BeautifulSoup import BeautifulSoup
from mechanize import ParseResponse, urlopen, urljoin
osm_base='http://www.openstreetmap.org/'
USE_FILE=True
def get_polygon_links_for_israel():
    home_israel_url='http://wiki.openstreetmap.org/wiki/IsraelPlaces'
    resp=urlopen(home_israel_url)
    soup=BeautifulSoup(''.join( resp.read()))
    results={}
    for row in soup('tr'):
        cols=row.findAll('td')
        if len(cols)<3:
            continue
        elif len(cols)>3:
            continue
        the_name,the_node,the_poly =cols
        if(len(the_poly('a'))==0):
            pass
        else:
            results[the_name.contents[0].strip()]=(the_poly('a',href=re.compile('http://www.openstreetmap.org/browse/way/'))[0])['href']
    return results
def explore_way(way_href):
    resp=urlopen(way_href)
    soup=BeautifulSoup(''.join( resp.read()))
    node_tags=soup('a',{'class':re.compile('node')})
    node_hrefs=[osm_base+tag['href'] for tag in node_tags]
    return node_hrefs
def node_href_check(text):
    print text
def get_coords_from_node(node_href):
    resp=urlopen(node_href)
    soup=BeautifulSoup(''.join( resp.read()))
    lat=soup.find('span',{'class':re.compile('latitude')}).contents[0]
    lng=soup.find('span',{'class':re.compile('longitude')}).contents[0]
    return float(lat),float(lng)
def main():
    if USE_FILE:
        polygon_map=eval(open('results').read())
    else:
        polygon_map=get_polygon_links_for_israel()
    results={}
    for place_name,polygon_link in  polygon_map.iteritems():
        print 'Getting node hrefs for', place_name
        results[place_name]= explore_way(polygon_link)
    file=open('polygons','w')
    print '*'*30
    for place_name,node_hrefs in results.iteritems():
        print 'Exploring polygon coords for', place_name,'- num of coords=',len(node_hrefs)
        line=place_name+":"+str([get_coords_from_node(node) for node in node_hrefs])+'\n'
        print line
        file.write(line)
if __name__ == '__main__':
    main()
    